package com.lqd.mall.sso.manager.factory;

import com.lqd.mall.basis.constant.Constants;
import com.lqd.mall.basis.constant.SecurityConstants;
import com.lqd.mall.basis.utils.LogUtils;
import com.lqd.mall.basis.utils.ServletUtils;
import com.lqd.mall.basis.utils.ip.AddressUtils;
import com.lqd.mall.basis.utils.ip.IpUtils;
import com.lqd.mall.interaction.system.SysLogininforEntity;
import com.lqd.mall.system.service.RemoteSysLogininforService;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 异步工厂（产生任务用）
 *
 * @author
 */

@Component
public class AsyncFactory {
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    public static   RemoteSysLogininforService remoteSysLogininforService;

    @Resource
    public void setRemoteSysLogininforService(RemoteSysLogininforService remoteSysLogininforService) {
        AsyncFactory.remoteSysLogininforService = remoteSysLogininforService;
    }

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息
     * @param args     列表
     * @return 任务task
     */
    public static SysLogininforEntity recordLogininfor(final String username, final String status, final String message,
                                                       final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());

        String address = AddressUtils.getRealAddressByIP(ip);
        StringBuilder s = new StringBuilder();
        s.append(LogUtils.getBlock(ip));
        s.append(address);
        s.append(LogUtils.getBlock(username));
        s.append(LogUtils.getBlock(status));
        s.append(LogUtils.getBlock(message));
        // 打印信息到日志
        sys_user_logger.info(s.toString(), args);
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        // 封装对象
        SysLogininforEntity logininfor = new SysLogininforEntity();
        logininfor.setUserName(username);
        logininfor.setIpaddr(ip);
        logininfor.setLoginLocation(address);
        logininfor.setBrowser(browser);
        logininfor.setOs(os);
        logininfor.setMsg(message);
        logininfor.setLoginTime(new Date());
        // 日志状态
        if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status)) {
            logininfor.setStatus(Constants.SUCCESS);
        } else if (Constants.LOGIN_FAIL.equals(status)) {
            logininfor.setStatus(Constants.FAIL);
        }
        // 插入数据
        //  SpringUtils.getBean(ISysLogininforService.class).insertLogininfor(logininfor);

        remoteSysLogininforService.saveLogininfor(logininfor, SecurityConstants.INNER);
        return logininfor;
    }
}
